package cn.qyd.threadpool;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author 邱运铎
 * 不使用Executors创建线程池，自定创建线程池实现简单web服务器
 * Web请求服务处理器，因为有频繁的网络IO操作，所以认为是io密集型任务，线程数设置 cpu核心数 * 2
 * @date 2024-03-23 0:43
 */
public class SimpleWebServerUpgrade {
    private final static int CPU_CORE_COUNT = Runtime.getRuntime().availableProcessors();
    private final static int CORE_POOL_SIZE = CPU_CORE_COUNT * 2;
    private final static int MAX_POOL_SIZE = CPU_CORE_COUNT * 2 + 1;

    private final static ExecutorService executor = new ThreadPoolExecutor(CORE_POOL_SIZE,
            MAX_POOL_SIZE,
            60L,
            TimeUnit.SECONDS,
            new LinkedBlockingQueue<>(1000));

    public static void main(String[] args) {
        while (true) {
            executor.execute(() -> System.out.println(Thread.currentThread().getName() + " 正在处理请求。。。"));
        }
    }
}
